package selenium.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.io.File;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

public class AddMoviePage extends AnyPage {

    @FindBy(name = "imdbsearch")
    private WebElement imdbsearchField;

    @FindBy(xpath = "//input[@value='Search']")
    private WebElement searchButton;

    @FindBy(name = "name")
    private WebElement nameField;

    @FindBy(name = "year")
    private WebElement yearField;

    @FindBy(name = "aka")
    private WebElement akaField;

    @FindBy(name = "duration")
    private WebElement durationField;

    @FindBy(name = "rating")
    private WebElement ratingField;

    @FindBy(name = "format")
    private WebElement formatsField;

    @FindBy(name = "loanname")
    private WebElement loannameField;

    @FindBy(name = "loandate")
    private WebElement loandateField;

    @FindBy(name = "trailer")
    private WebElement trailerField;

    @FindBy(name = "notes")
    private WebElement notesField;

    @FindBy(name = "taglines")
    private WebElement taglinesField;

    @FindBy(name = "languages")
    private WebElement languagesField;

    @FindBy(name = "plotoutline")
    private WebElement plotoutlineField;

    @FindBy(id = "own_yes")
    private WebElement ownYesField;

    @FindBy(id = "own_no")
    private WebElement ownNoField;

    @FindBy(id = "loaned_yes")
    private WebElement loanedYesField;

    @FindBy(id = "loaned_no")
    private WebElement loanedNoField;

    @FindBy(name = "submit")
    private WebElement submitButton;

    @FindBy(name = "cover")
    private WebElement cover;

    public AddMoviePage(PageManager addMoviePage) {
        super(addMoviePage);
    }

    public AddMoviePage ensurePageLoaded() {
        super.ensurePageLoaded();
        wait.until(presenceOfElementLocated(By.xpath("//h2[text()=\"Add from IMDb\"]")));
        return this;
    }

    public AddMoviePage setNameField(String name) {
        clearAndFill(nameField, name);
        return this;
    }

    public AddMoviePage setYearField(String year) {
        clearAndFill(yearField, year);
        return this;
    }

    public AddMoviePage setAkaField(String aka) {
        clearAndFill(akaField, aka);
        return this;
    }

    public AddMoviePage setDurationField(String duration) {
        clearAndFill(durationField, duration);
        return this;
    }

    public AddMoviePage setRatingField(String rating) {
        clearAndFill(ratingField, rating);
        return this;
    }

    public AddMoviePage setFormatsField(String formats) {
        clearAndFill(formatsField, formats);
        return this;
    }

    public AddMoviePage setLoannameField(String loanname) {
        clearAndFill(loannameField, loanname);
        return this;
    }

    public AddMoviePage setLoandateField(String loandate) {
        clearAndFill(loandateField, loandate);
        return this;
    }

    public AddMoviePage setTrailerField(String trailer) {
        clearAndFill(trailerField, trailer);
        return this;
    }

    public AddMoviePage setNotesField(String notes) {
        clearAndFill(notesField, notes);
        return this;
    }

    public AddMoviePage setTaglinesField(String taglines) {
        clearAndFill(taglinesField, taglines);
        return this;
    }

    public AddMoviePage setLanguagesField(String languages) {
        clearAndFill(languagesField, languages);
        return this;
    }

    public AddMoviePage setPlotoutlineField(String plotoutline) {
        clearAndFill(plotoutlineField, plotoutline);
        return this;
    }

    public AddMoviePage setOwnYes() {
        ownYesField.click();
        return this;
    }

    public AddMoviePage setOwnNo() {
        ownNoField.click();
        return this;
    }

    public AddMoviePage setLoanedYes() {
        loanedYesField.click();
        return this;
    }

    public AddMoviePage setLoanedNo() {
        loanedNoField.click();
        return this;
    }

    public void saveForm() {
        submitButton.click();
    }

    public WebElement getCoverField() {
        return cover;
    }

    public AddMoviePage setCoverField(String coverPath) {

        File file = new File(String.valueOf(coverPath));
        if (file.exists()) {
            cover.sendKeys(file.getPath());
        }

        return this;
    }

    public WebElement getImdbsearchField() {
        return imdbsearchField;
    }

    public AddMoviePage setImdbsearchField(String imdbsearch) {
        clearAndFill(imdbsearchField, imdbsearch);
        return this;
    }

    public AddMoviePage clickSearchButton() {
        searchButton.click();
        return this;
    }
}
